<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<div class="page-breadcrumbs">
    <ul class="breadcrumb">
        <li>
            <i class="fa fa-home"></i>
            <a href="#">Home</a>
        </li>
        <li>
            <a href="#!user/index">用户管理</a>
        </li>
        <li class="active">用户添加</li>
    </ul>
</div>
<div class="page-body">
    <div class="row">
        <div class="col-xs-12 col-md-12">
            <div class="widget">
                <div class="widget-header bordered-bottom bordered-themeprimary">
                    <i class="widget-icon fa fa-tasks themeprimary"></i>
                    <span class="widget-caption themeprimary">用户基本信息</span>
                </div>
                <div class="widget-body">
                    <div class="form-horizontal bv-form" novalidate="novalidate">
                        <form id="editForm">
                            <div class="form-group has-feedback">
                                <label class="col-lg-2 control-label">名称</label>
                                <div class="col-lg-9">
                                    <input type="text" class="form-control" name="name" data-bv-field="name"><i class="form-control-feedback" data-bv-field="name" style="display: none;"></i>
                                </div>
                            </div>
                            <div class="form-group has-feedback">
                                <label class="col-lg-2 control-label">密码</label>
                                <div class="col-lg-9">
                                    <input type="text" class="form-control" name="password" data-bv-field="password"><i class="form-control-feedback" data-bv-field="password" style="display: none;"></i>
                                </div>
                            </div>
                            <div class="form-group has-feedback">
                                <label class="col-lg-2 control-label">盐值</label>
                                <div class="col-lg-9">
                                    <input type="text" class="form-control" name="salt" data-bv-field="salt"><i class="form-control-feedback" data-bv-field="salt" style="display: none;"></i>
                                </div>
                            </div>
                            <div class="form-group has-feedback">
                                <label class="col-lg-2 control-label">头像</label>
                                <div class="col-lg-9">
                                    <input type="text" class="form-control" name="headPortrait" data-bv-field="headPortrait"><i class="form-control-feedback" data-bv-field="headPortrait" style="display: none;"></i>
                                </div>
                            </div>
                            <div class="form-group has-feedback">
                                <label class="col-lg-2 control-label">电话</label>
                                <div class="col-lg-9">
                                    <input type="text" class="form-control" name="phone" data-bv-field="phone"><i class="form-control-feedback" data-bv-field="phone" style="display: none;"></i>
                                </div>
                            </div>
                            <div class="form-group has-feedback">
                                <label class="col-lg-2 control-label">邮箱</label>
                                <div class="col-lg-9">
                                    <input type="email" class="form-control" name="email"
                                           data-bv-field="email"
                                           data-bv-emailaddress="true"
                                           data-bv-emailaddress-message="邮箱地址输入格式不对example@example.com"><i class="form-control-feedback" data-bv-field="email" style="display: none;"></i>
                                </div>
                            </div>
                            <div class="form-group has-feedback">
                                <label class="col-lg-2 control-label">备注</label>
                                <div class="col-lg-9">
                                    <textarea class="form-control" rows="5" name="note"></textarea>
                                </div>
                            </div>
                        </form>
                        <div class="form-group">
                            <div class="col-lg-8 col-lg-offset-4">
                                <button class="btn btn-blue" id="saveAdd">保存</button>
                                <button class="btn btn-default" id="cancel">取消</button>
                            </div>
                        </div>
                        <div/>
                    </div>
                </div>
            </div>
        </div>

    </div>
</div>
<script type="application/javascript">

    $(function(){

        $('#saveAdd').click(function(e){
            var data = $('#editForm').data('bootstrapValidator');
            if (data) {
                data.validate();
                if (data.isValid()) {
                    $.ajax({
                        type : "post",
                        url : "user/saveAdd",
                        contentType: 'application/json',
                        data: JSON.stringify($('#editForm').serializeJson()),
                        dataType: 'json',
                        success: function (response) {
                            Notify(response.message, 'top-right', '800', 'success', 'fa-bolt', true);
                            window.history.go(-1);
                        },
                        error: function(error) {
                            var obj = jQuery.parseJSON(error.responseText);
                            Notify(obj.message, 'top-right', '800', 'warning', 'fa-bolt', true);
                        }
                    });
                }
            }
        });

        $('#cancel').click(function(e){
            window.history.go(-1);
        });

        $('#editForm').bootstrapValidator({
            excluded: [':disabled'],
            feedbackIcons: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
            fields: {
                name: {
                    validators: {
                        notEmpty: {
                            message: '名称不能为空'
                        }
                    }
                },
                password:{
                	validators: {
                        notEmpty: {
                            message: '密码不能为空'
                        }
                    }  
                },
                salt:{
                	validators: {
                        notEmpty: {
                            message: '盐值不能为空'
                        }
                    } 
                },
                phone:{
                	validators: {
                        notEmpty: {
                            message: '电话不能为空'
                        }
                    } 
                },
                email: {
                	validators: {
                        notEmpty: {
                            message: '邮箱不能为空'
                        }
                    } 
                }
            }
        });
    });
</script>